import simulateApi from './simulateApi'

const state = {
    TreeData: [],
    TreeMappingCache: {},
    ActiveTreeList: [],
    ActiveTreeId: '',
    // List Id
    ListActiveId: null,
    // 源数据缓存对象
    AjaxTreeData: {},
    // 编辑表格对象，在获取数据时用于对比 并生成结果
    EditTreeData: {},
};
const getters = {

};
const mutations = {
    // 改变 Tree 与 List 的焦点状态, 用于获取 焦点数据
    SetListId(state) {
        var Have = false;
        for (let i = 0; i < state.ActiveTreeList.length; i++) {
            const element = state.ActiveTreeList[i];
            if (element.id == state.ActiveTreeId) {
                Have = true;
                break;
            }
        };
        if (Have) {
            state.ListActiveId = state.ActiveTreeId;
        } else {
            state.ListActiveId = null;
        }
    },
    // 组件用完，记得清空!!!!
    Clear(state) {
        state.TreeData = [];
        state.TreeMappingCache = {};
        state.ActiveTreeList = [];
        state.ActiveTreeId = '';
        state.ListActiveId = null;
        state.AjaxTreeData = {};
        state.EditTreeData = {};
    },
};
const actions = {
    SetTreeId({ state, commit }, id) {
        state.ActiveTreeId = id;
        commit('SetListId');
    },
    // 将列表数据 转换为页面用的 树形数据
    GetTreeData({ state }) {
        return new Promise((resolve) => {
            state.TreeData = [];
            var TreeCache = simulateApi.treeData()
            // 先将 数组 转为 Mapping 关系对象
            var MappingCache = {}
            for (let i = 0; i < TreeCache.length; i++) {
                const element = TreeCache[i]
                MappingCache[element.id] = element
            };
            // 缓存 Mapping 数据
            state.TreeMappingCache = MappingCache;
            var TreeData = []
            // 根据 ParentId 字段，将树列表转换为树形
            for (const key in MappingCache) {
                const element = MappingCache[key]
                if (element.parentId && MappingCache[element.parentId]) {
                    if (!MappingCache[element.parentId].children) {
                        // 此处做隔离准备，如果父级中没有子集，则创建子集对象
                        // 此处处理，可以通过对象属性中是否包含 'children' 对象来判断是否有子集
                        MappingCache[element.parentId].children = []
                    }
                    // 真正的子集赋值
                    MappingCache[element.parentId].children.push(element)
                } else {
                    TreeData.push(element)
                }
            }
            state.TreeData = TreeData
        })
    },
    // 树形数据 关键项加载
    getActiveData({ dispatch }) {
        // 伪造一个 Promise 来代表 ajax 数据获取
        return new Promise((resolve) => {
            // 利用缓存的树形数据，来加载列表和树形数据开关
            var activeData = simulateApi.activeData();
            dispatch('ChangeTreeActiveData', activeData);
            resolve(activeData);
        })
    },
    ChangeTreeActiveData({ state, commit }, data) {
        var ActiveData = []
        for (let i = 0; i < data.length; i++) {
            if (!data[i].children) {
                ActiveData.push(data[i].id)
            }
        }
        // 刷新树下方选中数据列表
        state.ActiveTreeList = [];
        for (let i = 0; i < ActiveData.length; i++) {
            const id = ActiveData[i];
            const treeItem = state.TreeMappingCache[id];
            if (treeItem) {
                state.ActiveTreeList.push({
                    title: treeItem.title,
                    id: id,
                });
            }
        };
        commit('SetListId');
    },
    GetTableData({ state }, id) {
        if (!id) return;
        var TableData = state.EditTreeData[id];
        if (!TableData) {
            var _tableData = simulateApi.tableData();
            _tableData.TreeData = state.TreeMappingCache[id];
            state.EditTreeData[id] = _tableData;
            // 此处做一次数据隔离,防止源数据被篡改，无法对比数据
            state.AjaxTreeData[id] = JSON.parse(JSON.stringify(_tableData));
        }
    },
    // 返回Table全数据
    GetValue({ state }) {
        return new Promise((resolve) => {
            resolve(JSON.parse(JSON.stringify(state.EditTreeData)));
        });
    },
    //对比数据 返回对Table 数据的更改
    GetValueByMerge({ state }) {
        return new Promise((resolve) => {
            const Mapping = {};
            for (const key in state.EditTreeData) {
                const EditData = state.EditTreeData[key];
                const AjaxData = state.AjaxTreeData[key];
                const MappingItem = [];
                for (let i = 0; i < EditData.data.length; i++) {
                    const EditItem = EditData.data[i];
                    const AjaxItem = AjaxData.data[i];
                    for (const key in EditItem) {
                        const EditValue = EditItem[key];
                        const AjaxValue = AjaxItem[key];
                        if (EditValue != AjaxValue) {
                            MappingItem.push(EditItem);
                            break;
                        }
                    }
                };
                if (MappingItem.length) {
                    Mapping[key] = MappingItem;
                };
            }
            resolve(JSON.parse(JSON.stringify(Mapping)));
        });
    }
};
export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}